package controler.states;
import controler.A_State;
import controler.A_UserCommand;
import controler.userCommands.AddToSelection;
import controler.userCommands.ChangeElementProperty;
import controler.userCommands.ChangeUserViewPoint;
import controler.userCommands.DeleteElement;
import controler.userCommands.EditPhysicShape;
import controler.userCommands.RemoveFromSelection;
import controler.userCommands.SwitchState;
import graphic.RenderObject;
import model.LevelEditorModelEvent;
import model.ModelUpdateType;
import view.events.LevelEditorViewKeyboardEvent;
import view.events.LevelEditorViewMouseEvent;
import view.events.LevelEditorViewOtherEvent;
import view.PropertyPanel;

/**
 * @author Damien Mabin
 */

class SimpleSelection extends A_State
{

	public function new() 
	{
		super();
		this.addEventListener(LevelEditorViewMouseEvent.EVENT, OnLevelEditorMouseEvent);
		this.addEventListener(LevelEditorViewKeyboardEvent.EVENT, OnLevelEditorKeyboardEvent);
		this.addEventListener(LevelEditorViewOtherEvent.EVENT, OnLevelEditorOtherEvent);
	}
	
	private function OnLevelEditorOtherEvent(_Evt:LevelEditorViewOtherEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case CHANGE_RENDER_OBJECT_PROPERTY(ro, type, value) :
			{	
				A_State.s_Controller.DoUserCommand(new ChangeElementProperty(null, RenderObject, ro, type, value));
			}
			case EDIT_RENDER_OBJECT_PHYSIC_BODY(ro) :
			{
				A_State.s_Controller.DoUserCommand(new RemoveFromSelection(null, RenderObject, ro));
				A_State.s_Controller.DoUserCommand(new EditPhysicShape(A_State.s_Controller.PHYSIC_SHAPE_EDITION, ro, true));
			}
			default :
			{
				
			}
		}
	}
	
	private function OnLevelEditorKeyboardEvent(_Evt:LevelEditorViewKeyboardEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case KEY_DOWN :
			{
				if (_Evt.m_KeyCode == 17 /*CTRL_KEY*/)
				{
					A_State.s_Controller.DoUserCommand(new SwitchState(A_State.s_Controller.MULTIPLE_SELECTION));
				}
				else if (_Evt.m_KeyCode == 46 /*Suppr*/)
				{
					var firstCommand:Bool = true;
					var selection : Array<RenderObject> =  A_State.s_Model.VUE_GetSelection(RenderObject).copy();
					while(selection.length > 0)
					{
						var cmd : A_UserCommand = new DeleteElement(null, RenderObject, selection.pop());
						if (!firstCommand)
						{
							cmd.m_Stackable = true;
						}
						else
						{
							firstCommand = false;
						}
						A_State.s_Controller.DoUserCommand(cmd);
					}
					var cmd : A_UserCommand = new SwitchState(A_State.s_Controller.INITIAL);
					if (!firstCommand)
					{
						cmd.m_Stackable = true;
					}
					else
					{
							firstCommand = false;
					}
					A_State.s_Controller.DoUserCommand(cmd);
				}
			}
			case KEY_UP :
			{
				
			}
		}
	}
	
	private function OnLevelEditorMouseEvent(_Evt:LevelEditorViewMouseEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case MOUSE_DOWN :
			{
				switch(_Evt.m_TargetType)
				{
					case RENDER_OBJECT :
					{
						if(!A_State.s_Controller.m_KeyboardKeyState[32/*space*/])
						{
							if (!A_State.s_Model.IsSelected(RenderObject, _Evt.m_Target))
							{
								var firstCommand:Bool = true;
								var selection : Array<RenderObject> =  A_State.s_Model.VUE_GetSelection(RenderObject).copy();
								while(selection.length > 0)
								{
									var cmd : A_UserCommand = new RemoveFromSelection(null, RenderObject, selection.pop());
									if (!firstCommand)
									{
										cmd.m_Stackable = true;
									}
									else
									{
										firstCommand = false;
									}
									A_State.s_Controller.DoUserCommand(cmd);
								}
								var cmd : A_UserCommand = new AddToSelection(null, RenderObject, _Evt.m_Target);
								if (!firstCommand)
								{
									cmd.m_Stackable = true;
								}
								else
								{
										firstCommand = false;
								}
								A_State.s_Controller.DoUserCommand(cmd);
							}
							else
							{
								A_State.s_Controller.DoUserCommand(new SwitchState(A_State.s_Controller.MOVE_SELECTION));
							}
						}
					}
					case SCENE :
					{
						var firstCommand:Bool = true;
						var selection : Array<RenderObject> =  A_State.s_Model.VUE_GetSelection(RenderObject).copy();
						while(selection.length > 0)
						{
							var cmd : A_UserCommand = new RemoveFromSelection(null, RenderObject, selection.pop());
							if (!firstCommand)
							{
								cmd.m_Stackable = true;
							}
							else
							{
								firstCommand = false;
							}
							A_State.s_Controller.DoUserCommand(cmd);
						}
						var cmd : A_UserCommand = new SwitchState(A_State.s_Controller.INITIAL);
						if (!firstCommand)
						{
							cmd.m_Stackable = true;
						}
						else
						{
								firstCommand = false;
						}
						A_State.s_Controller.DoUserCommand(cmd);
					}
					default :
					{
						
					}
				}
			}
			case MOUSE_MOVE :
			{
				if (A_State.s_Controller.m_KeyboardKeyState[32/*space*/])
				{
					A_State.s_Controller.DoUserCommand(new ChangeUserViewPoint(null, _Evt.m_MouseMovement.x, _Evt.m_MouseMovement.y, 0));
				}
			}
			default :
			{
				
			}
		}
	}
}